package com.njegovanobradovic.pages.admin;

import java.io.File;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;

import com.njegovanobradovic.entities.Media;
import com.njegovanobradovic.so.Save;

public class AddMedia {
	@Property
    private UploadedFile file;
	@Inject
	private Save<Media> uploadMedia;
 
	@CommitAfter
    public Object onSuccess(){
    	try {
            File copied = new File("src/main/webapp/layout/img/" + file.getFileName());
            file.write(copied);
            Media media = new Media();
            media.setPath("layout/img/" + file.getFileName());
            uploadMedia.setObject(media);
            uploadMedia.execute();
            return Library.class;
		} catch (Exception e) {
			return null;
		}
    }
}
